//2025-4-22

//多路选择逻辑

//当sel为00时，y是a和b的与
//当sel为01时，y是a和b的或
//当sel为10时，y是a和b的异或
//当sel为11时，y是a和b的同或

//使用case语句
//四选一逻辑

`timescale 1ns/100ps
module fn_sw_4(a,b,sel,y);

input a;
input b;
input[1:0] sel;  //sel有两位
output y;

reg y;
always @(a or b or sel) begin
    case(sel)
    2'b00:begin y <= a&b; end

    2'b01:begin y <= a|b; end

    2'b10:begin y <= a^b; end

    2'b11:begin/* y <= a~^b;*/ y <= ~(a^b); end
    endcase 
end

endmodule

//testbench of fn_sw_4_
module fn_sw_4_tb;
reg[3:0] absel;
wire y;

fn_sw_4 fn_sw_4(.a(absel[0]), .b(absel[1]), .sel(absel[3:2]), .y(y));

initial begin
    absel <= 0;
#200 $stop; //200ns后结束
end

always #10 absel <= absel+1; //每过10ns+1

endmodule